﻿using Microsoft.VisualStudio.TestTools.UnitTesting;

using DatabaseGroup.SSIS.SourceAdapters.Parser;

namespace DatabaseGroup.UnitTests.SSIS.SourceAdapters.Parser
{
    [TestClass()]
    public class RowDataTest
    {
        private TestContext _testContextInstance;

        public TestContext TestContext
        {
            get
            {
                return _testContextInstance;
            }
            set
            {
                _testContextInstance = value;
            }
        }

        [TestMethod()] public void ColumnCount_DefaultValue_ReturnDefaultZeroValue()
        {
            RowData target = new RowData();
            Assert.AreEqual<int>(0, target.ColumnCount, "Expected column count to return the default value of zero");
        }

        [TestMethod()]
        [ExpectedException(typeof(System.ArgumentException), "Expected ArgumentException to be thrown when called when an invalid column index")]
        public void GetColumnData_InvalidPositiveArg_ThrowArgumentException()
        {
            RowData target = new RowData();
            target.GetColumnData(3);
        }

        [TestMethod()]
        [ExpectedException(typeof(System.ArgumentException), "Expected ArgumentException to be thrown when called when an invalid column index")]
        public void GetColumnData_InvalidNegativeArg_ThrowArgumentException()
        {
            RowData target = new RowData();
            target.GetColumnData(-3);
        }

        [TestMethod()]
        public void ResetRowData_ResetTwoColumns_ReturnExpectedDefaultColumnCount()
        {
            RowData target = new RowData();
            target.AddColumnData("A");
            target.AddColumnData("B");
            Assert.AreEqual<int>(2, target.ColumnCount, "Expected column count to be 2 after adding 2 columns");
            target.ResetRowData();
            Assert.AreEqual<int>(0, target.ColumnCount, "Expected column count to be 0 after reseting row data");
        }

        [TestMethod()]
        public void AddColumnData_AddTwoColumns_ReturnExpectedColumnDataAndCount()
        {
            RowData target = new RowData();
            target.AddColumnData("ABC");
            Assert.AreEqual<int>(1, target.ColumnCount, "Expected column count to be 1 after adding 1 column");
            target.AddColumnData("DEF");
            Assert.AreEqual<int>(2, target.ColumnCount, "Expected column count to be 2 after adding 2 columns");
            Assert.AreEqual<string>("ABC", target.GetColumnData(0), "Expected column data to be same as data added for column 1");
            Assert.AreEqual<string>("DEF", target.GetColumnData(1), "Expected column data to be same as data added for column 2");
        }

        [TestMethod()]
        public void RowText_DefaultValue_ReturnExpectedDefaultValue()
        {
            RowData target = new RowData();
            Assert.AreEqual<string>(string.Empty, target.RowText, "Expected default value of empty string");
        }

        /// <summary>
        /// Verifies the column values within the row data match
        /// the list of expected ones provided
        /// </summary>
        /// <param name="rowData">The row data object containing the column values</param>
        /// <param name="expectedValues">The list of expected column values</param>
        internal static void VerifyParsedRow(RowData rowData, string[] expectedValues)
        {
            VerifyParsedRow(rowData, null, expectedValues);
        }

        /// <summary>
        /// Verifies the row and column data within the row data object
        /// match the expected ones provided
        /// </summary>
        /// <param name="rowData">The row data object containing the row/column values</param>
        /// <param name="expectedRowText">The expected row data</param>
        /// <param name="expectedValues">The list of expected column values</param>
        internal static void VerifyParsedRow(RowData rowData, string expectedRowText, string[] expectedValues)
        {
            Assert.AreEqual<int>(expectedValues.Length, rowData.ColumnCount, "Expected column count does not match the actual number of columns provided");

            if (!string.IsNullOrEmpty(expectedRowText))
            {
                Assert.AreEqual<string>(expectedRowText, rowData.RowText, "Expected row text does not match actual row text");
            }

            for (int i = 0; i < rowData.ColumnCount && i < expectedValues.Length; i++)
            {
                Assert.AreEqual<string>(expectedValues[i], rowData.GetColumnData(i), "Expected column value does not match the actual column value");
            }
        }
    }
}
